
/*
 *
 * Copyright (C) 2008 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using System.Collections.Generic;
using System;
using FlowLib.Containers;
using ClientExample.Containers;

namespace ClientExample.Comparers
{
    public class SearchResultGroupComparer : IComparer<SearchResultGroup>
    {
        [Flags]
        public enum Columns
        {
            ID,                 /* STRING */
            DisplayName,        /* STRING */
            Size,               /* INT */
            Sources,            /* INT */
            Type,               /* STRING */
        }

        public Columns SortOnColumn
        {
            get;
            set;
        }

        public bool Reverse
        {
            get;
            set;
        }

        public SearchResultGroupComparer()
        {
            SortOnColumn = Columns.ID;
        }

        public SearchResultGroupComparer(Columns sortOn)
        {
            SortOnColumn = sortOn;
            Reverse = false;
        }

        public int Compare(SearchResultGroup x, SearchResultGroup y)
        {
            if (Reverse)
            {
                SearchResultGroup z = x;
                x = y;
                y = z;
            }
            int compValue = -1;
            switch (SortOnColumn)
            {
                case Columns.ID:
                    compValue = x.ID.CompareTo(y.ID);
                    break;
                case Columns.DisplayName:
                    compValue = x.DisplayName.CompareTo(y.DisplayName);
                    break;
                case Columns.Size:
                    compValue = x.Size.CompareTo(y.Size);
                    break;
                case Columns.Sources:
                    compValue = x.Count.CompareTo(y.Count);
                    break;
                case Columns.Type:
                    compValue = x.DisplayType.CompareTo(y.DisplayType);
                    break;
            }
            if (SortOnColumn != Columns.ID && compValue == 0)
                compValue = x.ID.CompareTo(y.ID);
            return compValue;
        }
    }
}
